{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Deep Video Analytics tutorial"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Please run this notebook on Host OS or google cloud shell with dvactl configured & DVA running"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Make sure following modules can be imported succcessfully by running \n",
    "`pip install --upgrade requests pillow`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os, requests, sys, json, jsonschema\n",
    "from PIL import Image as PImage\n",
    "from IPython.display import Image, display"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### This tutorial uses REST API endpoint through DVA python client"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "sys.path.append('client/') #add DVA client to python path "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import dvaclient\n",
    "import logging, json"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Using Deep Video Analytics python client"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "####  REST API token generated by `dvactl start`  is stored in creds.json it can also be found at /security page in DVA UI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "creds = json.load(file('creds.json'))\n",
    "server, token = creds['server'], creds['token']\n",
    "# following line checks if the notebook is running \"inside\" the container\n",
    "if os.path.abspath('.') == '/root/DVA':\n",
    "    server = \"http://localhost/api/\"\n",
    "ctx = dvaclient.context.DVAContext(server=server,token=token)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## REST API vs DVAPQL"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Deep Video Analytics uses REST API (GET, LIST, but **NOT POST**) for exposing state of the system along metadata and urls to raw contents such as videos and images. **/queries is the only endpoint that supports POST requests** for mutating the state of the system. The user submits a DVAPQL query for loading videos/datasets, launching processing jobs, importing models and performing visual search. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### List videos"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "crnn_test with ID: 560d70e6-be51-4b9e-b652-e2bf299fc565\n",
      "exampledataset with ID: e01b4b33-6e96-457d-b1f7-720f7014396b\n",
      "coco_train_ci with ID: 9a958534-10da-4b14-8ef6-a6dc91e33d68\n",
      "citest with ID: ee5bfb74-adb2-4675-af14-e988e1829c2b\n",
      "livestream with ID: 319fcdb0-68c1-45d9-bbf2-8f5975c8b8d8\n",
      "spectre with ID: 7cead489-2ac7-414a-9dbe-79a09bcc24e5\n",
      "spectre with ID: 8c319efa-d8c8-49ab-a2ad-8a223505c3b4\n"
     ]
    }
   ],
   "source": [
    "for v in ctx.list_videos():\n",
    "    print \"{name} with ID: {vid}\".format(name=v['name'],vid=v['id'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### List configured indexing, detection and analysis models"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "coco D\n",
      "textbox D\n",
      "face D\n",
      "inception I\n",
      "facenet I\n",
      "crnn A\n",
      "tagger A\n",
      "Facenet_LOPQ_on_LFW P\n",
      "Inception_PCA_from_Youtube8M P\n"
     ]
    }
   ],
   "source": [
    "for v in ctx.list_models():\n",
    "    print v['name'], v['model_type']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## List events"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2018-08-04T22:54:35.484379Z 2018-08-04T22:54:35.483782Z perform_init True True 0.0\n",
      "2018-08-04T22:55:30.315301Z 2018-08-04T22:55:30.314943Z perform_launch True True 0.0\n",
      "2018-08-04T23:10:36.588681Z 2018-08-04T23:10:36.588123Z perform_launch True True 0.0\n",
      "2018-08-04T23:11:11.752733Z 2018-08-04T23:11:35.880398Z perform_indexing True True 10.367042\n",
      "2018-08-04T22:55:30.385110Z 2018-08-04T22:55:53.666255Z perform_import True True 0.843314\n",
      "2018-08-04T22:55:30.379033Z 2018-08-04T22:55:53.666355Z perform_import True True 0.834929\n",
      "2018-08-04T22:55:54.483268Z 2018-08-04T22:55:54.515338Z perform_dataset_extraction True True 0.077662\n",
      "2018-08-04T22:55:30.394052Z 2018-08-04T22:55:50.716932Z perform_process_monitoring True True 60.183073\n",
      "2018-08-04T22:55:30.390894Z 2018-08-04T22:55:53.666483Z perform_import True True 1.93403\n",
      "2018-08-04T22:55:54.586692Z 2018-08-04T22:55:54.616989Z perform_analysis True True 14.232323\n",
      "2018-08-04T22:55:57.937029Z 2018-08-04T22:56:03.197808Z perform_indexing True True 6.516518\n",
      "2018-08-04T23:10:53.449670Z 2018-08-04T23:11:01.707026Z perform_indexing True True 7.392323\n",
      "2018-08-04T23:10:53.397362Z 2018-08-04T23:10:53.412101Z perform_indexing True True 8.23701\n",
      "2018-08-04T23:14:31.201229Z 2018-08-04T23:14:31.200777Z perform_launch True True 0.0\n",
      "2018-08-04T23:10:36.642571Z 2018-08-04T23:10:36.662789Z perform_process_monitoring True True 180.3339\n",
      "2018-08-04T22:55:54.579160Z 2018-08-04T22:55:54.617269Z perform_indexing True True 4.998335\n",
      "2018-08-04T23:11:02.925037Z 2018-08-04T23:11:16.990065Z perform_indexing True True 18.836252\n",
      "2018-08-04T23:11:19.905178Z 2018-08-04T23:11:46.359440Z perform_indexing True True 19.886277\n",
      "2018-08-04T23:12:11.994256Z 2018-08-04T23:12:12.005400Z perform_indexing True True 4.300033\n",
      "2018-08-04T23:15:25.607469Z 2018-08-04T23:15:25.629619Z perform_index_approximation True True 0.326477\n",
      "2018-08-04T22:55:54.488147Z 2018-08-04T22:55:54.521973Z perform_dataset_extraction True True 0.092599\n",
      "2018-08-04T22:55:30.327583Z 2018-08-04T22:55:53.666834Z perform_import True True 1.146825\n",
      "2018-08-04T22:55:54.587222Z 2018-08-04T22:55:59.724494Z perform_indexing True True 1.389792\n",
      "2018-08-04T22:55:54.575747Z 2018-08-04T22:56:01.507984Z perform_detection True True 4.885495\n",
      "2018-08-04T22:55:54.609668Z 2018-08-04T22:56:08.938548Z perform_analysis True True 2.626708\n",
      "2018-08-04T22:55:54.560005Z 2018-08-04T22:55:54.577030Z perform_detection True True 29.619125\n",
      "2018-08-04T23:26:19.441737Z 2018-08-04T23:34:01.650028Z perform_indexing True True 261.534884\n",
      "2018-08-04T22:55:54.793645Z 2018-08-04T22:55:54.824429Z perform_video_segmentation True True 1.454032\n",
      "2018-08-04T22:55:55.563684Z 2018-08-04T22:55:55.615736Z perform_dataset_extraction True True 0.097087\n",
      "2018-08-04T22:55:55.698460Z 2018-08-04T22:56:01.489832Z perform_indexing True True 1.653967\n",
      "2018-08-04T22:55:54.561272Z 2018-08-04T22:55:54.577751Z perform_detection True True 16.124849\n",
      "2018-08-04T22:55:57.901826Z 2018-08-04T22:56:14.451729Z perform_detection True True 4.932082\n",
      "2018-08-04T22:55:55.674354Z 2018-08-04T22:56:12.437586Z perform_detection True True 1.924855\n",
      "2018-08-04T22:55:55.707348Z 2018-08-04T22:56:11.612060Z perform_analysis True True 4.502782\n",
      "2018-08-04T22:55:56.273040Z 2018-08-04T22:55:56.289197Z perform_video_decode True True 1.683314\n",
      "2018-08-04T23:23:36.337664Z 2018-08-04T23:37:29.086000Z perform_detection True True 524.342384\n",
      "2018-08-04T22:55:59.602115Z 2018-08-04T22:55:59.632928Z perform_index_approximation True True 0.140158\n",
      "2018-08-04T22:56:01.110567Z 2018-08-04T22:56:01.123536Z perform_index_approximation True True 0.054775\n",
      "2018-08-04T22:55:54.571915Z 2018-08-04T22:55:54.588553Z perform_detection True True 6.888583\n",
      "2018-08-04T22:56:03.138272Z 2018-08-04T22:56:03.155043Z perform_index_approximation True True 0.060945\n",
      "2018-08-04T22:56:01.465276Z 2018-08-04T22:56:01.493858Z perform_indexing True True 8.073191\n",
      "2018-08-04T22:56:09.546924Z 2018-08-04T22:56:09.577042Z perform_index_approximation True True 0.026353\n",
      "2018-08-04T22:56:06.389050Z 2018-08-04T22:56:09.670109Z perform_indexing True True 4.314435\n",
      "2018-08-04T22:56:09.599169Z 2018-08-04T22:56:14.025635Z perform_indexing True True 1.446219\n",
      "2018-08-04T22:55:57.965734Z 2018-08-04T22:56:16.169608Z perform_analysis True True 8.963307\n",
      "2018-08-04T22:55:55.683971Z 2018-08-04T22:56:06.443695Z perform_detection True True 3.159555\n",
      "2018-08-04T22:56:09.693086Z 2018-08-04T22:56:09.739548Z perform_index_approximation True True 0.114768\n",
      "2018-08-04T22:55:54.567786Z 2018-08-04T22:56:10.766606Z perform_detection True True 1.313136\n",
      "2018-08-04T22:56:13.975203Z 2018-08-04T22:56:13.995227Z perform_index_approximation True True 0.125513\n",
      "2018-08-04T22:56:15.462229Z 2018-08-04T22:56:15.477947Z perform_index_approximation True True 0.099559\n",
      "2018-08-04T22:56:10.687803Z 2018-08-04T22:56:10.715425Z perform_indexing True True 9.811037\n",
      "2018-08-04T22:56:20.520704Z 2018-08-04T22:56:20.535997Z perform_index_approximation True True 0.175447\n",
      "2018-08-04T22:56:24.596995Z 2018-08-04T22:56:24.621582Z perform_indexing True True 3.248104\n",
      "2018-08-04T22:56:14.336511Z 2018-08-04T22:56:28.196532Z perform_indexing True True 3.195461\n",
      "2018-08-04T22:56:19.377949Z 2018-08-04T22:56:31.410646Z perform_indexing True True 7.210144\n",
      "2018-08-04T23:28:32.777591Z 2018-08-04T23:28:32.795401Z perform_indexing True True 113.194299\n",
      "2018-08-04T23:10:36.605657Z 2018-08-04T23:10:36.652235Z perform_import True True 3.80524\n",
      "2018-08-04T22:56:24.179197Z 2018-08-04T22:56:24.203353Z perform_analysis True True 1.968337\n",
      "2018-08-04T22:56:12.068366Z 2018-08-04T22:56:20.720280Z perform_indexing True True 7.428708\n",
      "2018-08-04T22:56:28.145589Z 2018-08-04T22:56:28.159139Z perform_index_approximation True True 0.028434\n",
      "2018-08-04T23:10:40.453494Z 2018-08-04T23:10:40.467335Z perform_video_segmentation True True 8.879261\n",
      "2018-08-04T23:10:54.282659Z 2018-08-04T23:11:11.803167Z perform_detection True True 8.105931\n",
      "2018-08-04T23:10:54.257609Z 2018-08-04T23:11:09.140824Z perform_indexing True True 7.785359\n",
      "2018-08-04T23:11:36.994598Z 2018-08-04T23:11:37.006772Z perform_indexing True True 1.369834\n",
      "2018-08-04T23:12:37.177730Z 2018-08-04T23:12:37.190075Z perform_indexing True True 1.746356\n",
      "2018-08-04T22:55:57.924998Z 2018-08-04T22:56:09.651891Z perform_detection True True 14.950029\n",
      "2018-08-04T22:56:27.865413Z 2018-08-04T22:56:27.878920Z perform_index_approximation True True 0.041475\n",
      "2018-08-04T23:31:14.697268Z 2018-08-04T23:31:14.728657Z perform_index_approximation True True 1.577357\n",
      "2018-08-04T22:56:31.388817Z 2018-08-04T22:56:31.401988Z perform_index_approximation True True 0.028733\n",
      "2018-08-04T23:10:49.339325Z 2018-08-04T23:10:49.352752Z perform_video_decode True True 4.973449\n",
      "2018-08-04T23:10:49.343431Z 2018-08-04T23:10:49.356746Z perform_video_decode True True 4.05945\n",
      "2018-08-04T23:10:53.405649Z 2018-08-04T23:10:53.419080Z perform_detection True True 9.510662\n",
      "2018-08-04T23:10:54.297478Z 2018-08-04T23:12:12.092935Z perform_detection True True 25.088066\n",
      "2018-08-04T23:10:49.334522Z 2018-08-04T23:10:49.349172Z perform_video_decode True True 4.133282\n",
      "2018-08-04T23:37:26.337549Z 2018-08-04T23:37:26.356641Z perform_indexing True True 86.203397\n",
      "2018-08-04T23:40:05.954686Z 2018-08-04T23:40:05.977068Z perform_import True True 6.06904\n",
      "2018-08-04T23:40:12.041050Z 2018-08-04T23:40:12.059511Z perform_video_segmentation True True 18.80304\n",
      "2018-08-04T23:46:13.423592Z 2018-08-04T23:46:13.458546Z perform_indexing True True 85.999139\n",
      "2018-08-04T23:10:53.457425Z 2018-08-04T23:11:02.990216Z perform_detection True True 8.766642\n",
      "2018-08-04T23:38:23.180269Z 2018-08-04T23:38:23.195893Z perform_index_approximation True True 3.653911\n",
      "2018-08-04T23:40:30.825012Z 2018-08-04T23:40:30.842481Z perform_video_decode True True 8.446017\n",
      "2018-08-04T23:40:30.850321Z 2018-08-04T23:40:30.872968Z perform_video_decode True True 9.283066\n",
      "2018-08-04T23:24:44.644409Z 2018-08-04T23:46:14.742974Z perform_detection True True 130.822544\n",
      "2018-08-04T23:48:25.561234Z 2018-08-04T23:48:25.572845Z perform_indexing True True 21.789656\n",
      "2018-08-04T23:10:53.412500Z 2018-08-04T23:10:53.426816Z perform_detection True True 43.571358\n",
      "2018-08-04T23:10:53.473300Z 2018-08-04T23:11:37.080173Z perform_detection True True 34.917424\n",
      "2018-08-04T23:40:30.831353Z 2018-08-04T23:40:30.869569Z perform_video_decode True True 9.826122\n",
      "2018-08-04T23:26:58.582273Z 2018-08-04T23:38:44.317245Z perform_indexing True True 53.972367\n",
      "2018-08-04T23:40:40.688536Z None perform_detection False False -1.0\n",
      "2018-08-04T23:40:40.666717Z 2018-08-04T23:41:02.046623Z perform_indexing True True 11.370746\n",
      "2018-08-04T23:40:40.675996Z 2018-08-04T23:41:04.761195Z perform_detection True True 12.726716\n",
      "2018-08-04T23:49:09.656171Z 2018-08-04T23:49:09.670865Z perform_indexing True True 5.779241\n",
      "2018-08-04T23:39:38.284936Z 2018-08-04T23:39:38.299502Z perform_index_approximation True True 0.200897\n",
      "2018-08-04T23:14:43.179244Z 2018-08-04T23:14:43.200788Z perform_detection True True 65.219902\n",
      "2018-08-04T23:40:40.104036Z 2018-08-04T23:40:50.667712Z perform_indexing True True 11.322933\n",
      "2018-08-04T23:14:31.229700Z 2018-08-04T23:14:31.249372Z perform_process_monitoring True False -1.0\n",
      "2018-08-04T23:14:43.148385Z 2018-08-04T23:14:43.168370Z perform_detection True True 390.862941\n",
      "2018-08-04T23:14:41.650957Z 2018-08-04T23:14:41.696918Z perform_video_decode True True 1.491984\n",
      "2018-08-04T23:14:43.133236Z 2018-08-04T23:14:43.148925Z perform_detection True True 15.080233\n",
      "2018-08-04T23:14:43.159610Z 2018-08-04T23:14:43.181179Z perform_indexing True True 15.896353\n",
      "2018-08-04T23:14:43.141337Z 2018-08-04T23:14:43.155368Z perform_analysis True True 28.081707\n",
      "2018-08-04T23:14:58.220094Z 2018-08-04T23:14:59.132481Z perform_indexing True True 26.47948\n",
      "2018-08-04T23:15:48.416006Z 2018-08-04T23:15:48.431072Z perform_indexing True True 7.51214\n",
      "2018-08-04T23:40:05.926834Z 2018-08-04T23:40:05.926176Z perform_launch True True 0.0\n",
      "2018-08-04T23:16:15.305716Z 2018-08-04T23:16:15.343874Z perform_detection True True 737.437774\n",
      "2018-08-04T23:21:14.022386Z 2018-08-04T23:21:14.046653Z perform_analysis True True 448.932227\n",
      "2018-08-04T23:16:00.658915Z 2018-08-04T23:16:00.676160Z perform_video_decode True True 14.637934\n",
      "2018-08-04T23:16:15.293840Z 2018-08-04T23:16:15.315177Z perform_indexing True True 133.434869\n",
      "2018-08-04T23:40:39.233219Z 2018-08-04T23:40:39.249271Z perform_indexing True True 11.361266\n",
      "2018-08-04T23:16:15.255677Z 2018-08-04T23:16:15.273014Z perform_detection True True 126.687106\n",
      "2018-08-04T23:16:15.265638Z 2018-08-04T23:16:15.285884Z perform_analysis True True 257.138668\n",
      "2018-08-04T23:16:15.272625Z 2018-08-04T23:21:15.367702Z perform_detection True False -1.0\n",
      "2018-08-04T23:18:21.955681Z 2018-08-04T23:18:30.035954Z perform_indexing True True 294.637285\n",
      "2018-08-04T23:19:00.348030Z 2018-08-04T23:28:34.191876Z perform_detection True True 532.150326\n",
      "2018-08-04T23:21:05.838068Z 2018-08-04T23:25:53.642733Z perform_indexing True True 321.074637\n",
      "2018-08-04T23:19:00.300368Z None perform_detection False False -1.0\n",
      "2018-08-04T23:18:34.763637Z 2018-08-04T23:18:34.780062Z perform_video_decode True True 25.578337\n",
      "2018-08-04T23:19:00.322921Z 2018-08-04T23:23:27.426740Z perform_indexing True True 145.802464\n",
      "2018-08-04T23:19:00.285722Z 2018-08-04T23:20:33.818225Z perform_analysis True True 272.384269\n",
      "2018-08-04T23:40:39.241722Z 2018-08-04T23:40:39.271454Z perform_detection True True 12.339037\n",
      "2018-08-04T23:40:51.605790Z 2018-08-04T23:41:13.471035Z perform_indexing True True 16.491229\n",
      "2018-08-04T23:40:05.963929Z 2018-08-04T23:40:05.983628Z perform_process_monitoring True False -1.0\n",
      "2018-08-04T23:40:39.274768Z 2018-08-04T23:48:25.937631Z perform_detection True True 43.723135\n",
      "2018-08-04T23:19:00.243659Z 2018-08-04T23:19:00.263456Z perform_detection True True 125.578773\n",
      "2018-08-04T23:23:36.308554Z 2018-08-04T23:31:20.989664Z perform_indexing True True 108.657926\n",
      "2018-08-04T23:23:36.207580Z 2018-08-04T23:23:36.260882Z perform_detection True True 163.185681\n",
      "2018-08-04T23:23:36.265840Z 2018-08-04T23:25:09.086780Z perform_analysis True True 322.25898\n",
      "2018-08-04T23:23:24.664849Z 2018-08-04T23:23:24.684249Z perform_index_approximation True True 6.115708\n",
      "2018-08-04T23:23:36.294123Z None perform_detection False False -1.0\n",
      "2018-08-04T23:40:40.115600Z 2018-08-04T23:40:51.667831Z perform_detection True True 12.788923\n",
      "2018-08-04T23:40:40.132333Z 2018-08-04T23:49:09.773648Z perform_detection True False -1.0\n",
      "2018-08-04T23:41:04.446743Z 2018-08-04T23:41:30.005007Z perform_indexing True True 27.531317\n",
      "2018-08-04T23:22:51.200292Z 2018-08-04T23:22:51.219955Z perform_video_decode True True 45.12176\n",
      "2018-08-04T23:14:31.219598Z 2018-08-04T23:14:31.250158Z perform_stream_capture True True 600.852094\n",
      "2018-08-04T23:24:44.590074Z None perform_detection False False -1.0\n",
      "2018-08-04T23:24:32.096392Z 2018-08-04T23:24:32.113719Z perform_video_decode True True 12.554152\n",
      "2018-08-04T23:24:44.618089Z 2018-08-04T23:33:20.542557Z perform_indexing True True 26.792548\n",
      "2018-08-04T23:24:44.539260Z 2018-08-04T23:26:20.709866Z perform_detection True True 37.890899\n",
      "2018-08-04T23:24:44.553237Z 2018-08-04T23:30:32.607843Z perform_analysis True True 70.697018\n",
      "2018-08-04T23:41:17.483334Z 2018-08-04T23:41:57.618841Z perform_indexing True True 30.943813\n"
     ]
    }
   ],
   "source": [
    "_ = ctx.list_events(verbose=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Process a video"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "video_processing_query = dvaclient.query.ProcessVideoURL(name=\"spectre\",url=\"https://www.youtube.com/watch?v=ashLaclKCik\")\n",
    "video_processing_query.execute(ctx)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Optionally you can \"wait\" on it to complete,"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "video_processing_query.wait(verbose=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Finding similar images"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Query image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "leo_query_image = 'tests/data/query_leo.png'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Get configured retrievers and indexers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "indexers = {r['name']:r for r in ctx.list_models() if r['model_type'] == 'I'}\n",
    "retrievers = {r['name']:r for r in ctx.list_retrievers()}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Make a query using inception retriever and inception indexer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Timed out after 180 seconds\n"
     ]
    },
    {
     "ename": "KeyError",
     "evalue": "'query_results'",
     "traceback": [
      "\u001b[0;31m\u001b[0m",
      "\u001b[0;31mKeyError\u001b[0mTraceback (most recent call last)",
      "\u001b[0;32m<ipython-input-7-8f49dc3087ea>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      4\u001b[0m \u001b[0mq\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mexecute\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcontext\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mctx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      5\u001b[0m \u001b[0mq\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwait\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 6\u001b[0;31m \u001b[0mq\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgather_search_results\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m/root/DVA/client/dvaclient/query.pyc\u001b[0m in \u001b[0;36mgather_search_results\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m     60\u001b[0m         \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     61\u001b[0m             \u001b[0;32mfor\u001b[0m \u001b[0mt\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mresults\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'tasks'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 62\u001b[0;31m                 \u001b[0;32mif\u001b[0m \u001b[0mt\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'query_results'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     63\u001b[0m                     \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msearch_results\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mvisual_search_results\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mVisualSearchResults\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtask\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mt\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     64\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mKeyError\u001b[0m: 'query_results'"
     ],
     "output_type": "error"
    }
   ],
   "source": [
    "inception_indexer_pk = indexers['inception']['id']\n",
    "inception_retriever_pk = retrievers['inception']['id']\n",
    "q = dvaclient.query.FindSimilarImages(leo_query_image,indexer_pk=inception_indexer_pk,retriever_pk=inception_retriever_pk)\n",
    "q.execute(context=ctx)\n",
    "q.wait()\n",
    "q.gather_search_results()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Visualize results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "for r in q.search_results:\n",
    "    r.visualize()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "q = dvaclient.query.FindSimilarImages('tests/data/query_car.png',indexer_pk=inception_indexer_pk,retriever_pk=inception_retriever_pk)\n",
    "q.execute(context=ctx)\n",
    "q.wait()\n",
    "q.gather_search_results()\n",
    "for r in q.search_results:\n",
    "    r.visualize()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
